package com.one.group.user.controller;

import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.AdvertUpdateDto;
import com.one.group.model.vo.AdvertListVo;
import com.one.group.result.Result;
import com.one.group.service.AdvertService;
import com.one.group.user.service.AdvertCacheService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wanbo
 * @Date 2025/10/24 17:22
 */
@RestController
@RequestMapping("/advert")
@Slf4j
@Tag(name = "广告相关接口")
public class AdvertController {

    @DubboReference
    private AdvertService advertService;

    @Autowired
    private AdvertCacheService advertCacheService;

    // 此接口只能用于管理员修改  只修改广告位置
    @PostMapping("/updateAdvert")
    @Operation(summary = "更新广告")
    public Result updateAdvert(@RequestBody AdvertUpdateDto advertUpdateDto){
        // 添加空值检查
        if (advertUpdateDto == null) {
            log.error("接收到空的广告更新请求");
            return ResultUtil.error(ErrorCode.PARAMS_ERROR, "参数错误");
        }

        advertService.updateAdvert(advertUpdateDto);
        return ResultUtil.success();
    }

    @GetMapping("/findAdvertListByCell")
    @Operation(summary = "查询指定小区下的广告")
    public Result findAdvertListByCell(@RequestParam String cellId){
        // 添加空值检查
        if (cellId == null) {
            log.error("接收到空的查询小区下的广告请求");
            return ResultUtil.error(ErrorCode.PARAMS_ERROR, "参数错误");
        }

        AdvertListVo advertList = advertCacheService.findAdvertListByCellId(cellId);
        if (advertList.getCount()==0) {
            log.error("查询小区下的广告暂无");
            return ResultUtil.error(ErrorCode.NOT_FOUND_ERROR, "该小区下暂无广告位");
        }
        return ResultUtil.success(advertList);
    }


}
